//
//  ACLiveViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/3/29.
//

#import "ACLiveViewController.h"
#import "ACLiveListCell.h"
#import "ACLiveModel.h"
#import "ACAudienceViewController.h"
#import "ACAudienceViewController+LiveDelegate.h"

@interface ACLiveViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)TRTCLiveRoom *liveRoom;
@property(nonatomic,strong)NSMutableArray<ACLiveModel *>*dataArray;
@property(nonatomic,assign)int page;
@end

@implementation ACLiveViewController
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.estimatedRowHeight = 260;
        _tableview.backgroundColor = UIColorFromRGB(0xF4F5F8);
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        NSBundle *curB = [NSBundle BundleWithBundleName:ACLiteAVUISDK targetClass:[self class] ];
        [_tableview registerNib:[UINib nibWithNibName:@"ACLiveListCell" bundle:curB] forCellReuseIdentifier:@"ACLiveListCell"];
    }
    return _tableview;
}
- (NSMutableArray<ACLiveModel *> *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    self.page = 1;
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    @weakify(self);
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.page = 1;
        [self requestData];
    }];
    self.tableview.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.page += 1;
        [self requestData];
    }];
    [self.tableview.mj_header beginRefreshing];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableview.mj_header beginRefreshing];
}
-(void)requestData{
    [SVProgressHUD showWithStatus:@""];
    NSString *page = [NSString stringWithFormat:@"%d",self.page];
    [ACLiveRoomManager requestLiveListWithPage:page success:^(id  _Nonnull responseObject) {
        NSArray *dataArr = [responseObject valueForKey:@"data"];
        if (self.page == 1) {
            [self.dataArray removeAllObjects];
            self.dataArray = [ACLiveModel mj_objectArrayWithKeyValuesArray:dataArr];
        }else{
            [self.dataArray addObjectsFromArray:[ACLiveModel mj_objectArrayWithKeyValuesArray:dataArr]];
        }
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        [self.tableview reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSError * _Nonnull error) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
        [SVProgressHUD showInfoWithStatus:@"网络错误"];
    }];
}
-(void)requestToBookWithliveID:(NSString *)liveID{
    [ACLiveRoomManager requestBookLiveWithLiveID:liveID requestCallback:^(int code, NSString * _Nullable message) {
        if (code == 0) {
            [self.tableview.mj_header beginRefreshing];
            [SVProgressHUD showInfoWithStatus:@"预约成功"];
        }
    }];
}
-(void)requestJoinLiveRoomWithLiveID:(NSString *)liveID{
    [ACLiveRoomManager requestCheckAuthorityWithLiveID:liveID requestCallback:^(int code, NSString * _Nullable message) {
        if (code == 0) {
            ACAudienceViewController *VC = [[ACAudienceViewController alloc] init];
            [self.navigationController pushViewController:VC animated:YES];
            [VC loginWithRoomID:liveID callback:^(int code, NSString * _Nullable message) {
                
            }];
        }
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ACLiveListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACLiveListCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[ACLiveListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ACLiveListCell"];
    }
    ACLiveModel *liveModel = self.dataArray[indexPath.row];
    cell.liveModel = liveModel;
    @weakify(self);
    cell.bookActionHandler = ^{
        @strongify(self)
        if ([liveModel.status intValue] == 3) {
            [self requestJoinLiveRoomWithLiveID:liveModel.id_live];
        }else if ([liveModel.status intValue] == 1){
            [self requestToBookWithliveID:liveModel.id_live];
        }
    };
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - JXCategoryListContentViewDelegate

/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}

@end
